package com.wan37.auth.handler;

/**
 * @author : luoyong
 * @date : 2020-05-27 17:33
 **/

import com.google.protobuf.Message;
import com.wan37.auth.component.AuthWorkerHandlerCenter;
import com.wan37.auth.global.HandlerManager;
import com.wan37.protobuf.analysis.ParseMap;
import com.wan37.protobuf.message.internal.Internal;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * AuthServer的消息处理器.
 * 每一个连接对应一个handler.
 * 注意：AuthServer只保持和GateServer的连接.
 */

public class AuthServerHandler extends SimpleChannelInboundHandler<Message> {

    private static final Logger logger = LoggerFactory.getLogger(AuthServerHandler.class);
    /**
     * 认证中心
     */
    private AuthWorkerHandlerCenter authWorkerHandlerCenter;

    /**
     * gate连接的ctx
     */
    private static ChannelHandlerContext gateConnCtx;


    public AuthServerHandler(AuthWorkerHandlerCenter authWorkerHandlerCenter) {
        this.authWorkerHandlerCenter = authWorkerHandlerCenter;
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        //logger.info("[AuthServer] received connection from [GateServer].");
    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        logger.info("The connection with gateServer is inactive.");
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {

        //authServer收到gateServer发来的消息, 进行处理
        Internal.GTransfer gt = (Internal.GTransfer) msg;
        //获取ptoNum
        int ptoNum = gt.getPtoNum();
        //根据对应的ptoNum获取对应类型的message
        Message message = ParseMap.getMessage(ptoNum, gt.getMsg().toByteArray());

        //消息处理器
        MsgHandler msgHandler = null;

        //如果是Greet类型的消息
        if (message instanceof Internal.Greet) {

            logger.info("[AuthServer] received [Greet] message from [GateServer].");
            //处理Greet请求
            msgHandler = HandlerManager.getMsgHandler(ptoNum, gt.getUserId(), gt.getNetId(), message, ctx);
        } else {
            logger.info("[AuthServer] received [{}] message from [GateServer].", message.getClass().getSimpleName());
            //处理认证请求
            msgHandler = HandlerManager.getMsgHandler(ptoNum, gt.getUserId(), gt.getNetId(), message, getGateConnCtx());
        }

        //将消息分发给AuthCenter中的Worker线程进行处理
        authWorkerHandlerCenter.distributeWork(gt.getUserId(), msgHandler);

    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        throw new RuntimeException("[AuthServerHandler] caught an exception. ", cause);
    }


    public static ChannelHandlerContext getGateConnCtx() {
        return AuthServerHandler.gateConnCtx;
    }

    public static void setGateConnCtx(ChannelHandlerContext gateConnCtx) {
        AuthServerHandler.gateConnCtx = gateConnCtx;
    }
}
